﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ColdSun.Commons;
using ColdSun.Commons.Configs;
using ColdSun.Commons.Entities;
using ColdSun.Services;
using System.IO;
using ColdSun.Framework;

namespace ColdSun.BankManagements
{
    public partial class CtrlServiceDeskUser : UserControl
    {
        private FrmProgressBar frmProgressBar = new FrmProgressBar(); 
        private string infoFile;
        private string wordTemplateFile;

        public CtrlServiceDeskUser()
        {
            InitializeComponent();
        }

        private void CtrlServiceDeskUser_Load(object sender, EventArgs e)
        {
            txtOriginalFile.Text = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserOPOriginalExcelFile);            
            txtExportFolder.Text = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserExportFolder);
            dtpReplaceDate.Format = DateTimePickerFormat.Custom;
            dtpReplaceDate.CustomFormat = "dd/MM/yyyy";
            cmbType.SelectedIndex = 0;

            txtExportWordFile.Text = CommonConfigs.ServiceDeskUserOPExportFile;
            this.infoFile = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserOPInfoFile);
            this.wordTemplateFile = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserOPWordTemplateFile);
        }

        private void ReplaceParamsInDoc()
        {
            Replacement replace = new Replacement();
            replace.SheetName = "Sheet1";
            replace.Details = new List<ReplacementDetail>();
            replace.Details.Add(new ReplacementDetail());
            replace.Details[0].ColumnName = "ColumnName";
            replace.Details[0].RowIndex = 1;
            replace.Details[0].ReplaceParam = "param";

            string result = XmlUtility.SerializeObject<Replacement>(replace);
            MessageBox.Show(result);
        }

        private void btnOriginalFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog.Filter = "Excel Files (.xlsx)|*.xlsx|Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtOriginalFile.Text = openFileDialog.FileName;
            }
        }

        private void btnReplaceExportFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtExportFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundReplaceWorker_RunWorkerCompleted);

            // Kick off the Async thread
            backgroundWorker.RunWorkerAsync();

            // Lock up the UI with this modal progress form.
            frmProgressBar.ShowDialog();
        }

        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Logger.Info(string.Format("----- BEGIN TO PROCESS SERVICE DESK USER -----"));
            List<Replacement> replacementInfos = Utilities.GetReplacementInfo(this.infoFile);
            try
            {
                ServiceDeskUserService service = new ServiceDeskUserService();
                string result = service.GenerateServiceDeskUser(
                    txtOriginalFile.Text, this.wordTemplateFile, replacementInfos,
                    dtpReplaceDate.Value, txtExportWordFile.Text, txtExportFolder.Text);
            }
            catch (Exception ex)
            {
                Logger.Error("Background worker - Do work", ex);
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
            finally
            {
                Logger.Info(string.Format("----- END TO PROCESS SERVICE DESK USER -----"));
            }
        }

        private void BackgroundReplaceWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (frmProgressBar != null)
            {
                frmProgressBar.Close();
            }
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbType.SelectedIndex == 0)
            {
                txtExportWordFile.Text = CommonConfigs.ServiceDeskUserOPExportFile;
                this.infoFile = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserOPInfoFile);
                this.wordTemplateFile = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserOPWordTemplateFile);
            }
            else
            {
                txtExportWordFile.Text = CommonConfigs.ServiceDeskUserITExportFile;
                this.infoFile = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserITInfoFile);
                this.wordTemplateFile = Utilities.GetFullFileName(CommonConfigs.ServiceDeskUserITWordTemplateFile);
            }
        }

       
    }
}
